package com.dense.kuiniu.bookborrow.xjSetting.manager;

import android.content.Intent;
import android.os.Build;
import android.os.Environment;

import com.dense.kuiniu.bookborrow.App;
import com.dense.kuiniu.bookborrow.setting.ScreenImageActivity;
import com.dense.kuiniu.bookborrow.xjSetting.config.ShareKeyConfig;
import com.dense.kuiniu.bookborrow.xjSetting.listener.ScreenImageCutListener;
import com.dense.kuiniu.bookborrow.xjSetting.utils.SharePreferceUtil;

import java.io.File;
import java.lang.ref.WeakReference;

/**
 * 屏保展示管理器
 */
public class PingBaoManager {

    private static PingBaoManager install;
    private boolean stop = false;
    private WeakReference<ScreenImageCutListener> listener;

    public static PingBaoManager getInstance() {
        if (install == null) {
            synchronized (PingBaoManager.class) {
                if (install == null) {
                    install = new PingBaoManager();
                }
            }
        }
        return install;
    }
    private boolean openPingbao;
    private int cutTime;
    private int openTime;
    private boolean isOpenPingbao=true;

    public void setOpenPingbao(boolean openPingbao) {
        isOpenPingbao = openPingbao;
    }

    public void setListener(ScreenImageCutListener listener) {
        this.listener = new WeakReference<>(listener);
    }
    public void initData(){
        openPingbao= SharePreferceUtil.getInstance(App.getContext()).getBoolean(ShareKeyConfig.PING_BAO_OPEN);
        cutTime= SharePreferceUtil.getInstance(App.getContext()).getInt(ShareKeyConfig.PING_BAO_CUT_TIME,60000);
        openTime= SharePreferceUtil.getInstance(App.getContext()).getInt(ShareKeyConfig.PING_BAO_OPEN_TIME,30000);
    }
    public PingBaoManager() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!stop) {
                    if (openPingbao&&isOpenPingbao) {
                        long curTime = System.currentTimeMillis();
                        if (listener == null || listener.get() == null) {
                            //没有进入屏保界面
                            if (curTime - lastClick > openTime) {
                                //进入屏保
                                lastScreenCutTime = System.currentTimeMillis();
                                App.getContext().startActivity(new Intent(App.getContext(), ScreenImageActivity.class));
                            }
                        } else {
                            if (curTime - lastScreenCutTime > cutTime) {
                                //切换屏保
                                listener.get().onCut();
                                lastScreenCutTime = System.currentTimeMillis();
                            }
                        }
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        savePath();
    }

    public File savePath(){
        String grmPath="";
        if (Build.VERSION.SDK_INT >= 29) {
            grmPath = App.getContext().getExternalFilesDir(null).getAbsolutePath() ;
        }else {
            grmPath= Environment.getExternalStorageDirectory()
                    .getAbsolutePath() ;
        }
        grmPath+= "/borrow/screen";
        File file=new File(grmPath);
        if (!file.exists()){
            file.mkdirs();
        }
        return file;
    }

    private long lastClick = System.currentTimeMillis();
    private long lastScreenCutTime = System.currentTimeMillis();

    /**
     * 时间毫秒
     * @return
     */
    public int getCutTime() {
        return cutTime;
    }

    /**
     * 切换时间毫秒
     * @param cutTime
     */
    public void setCutTime(int cutTime) {
        this.cutTime = cutTime;
         SharePreferceUtil.getInstance(App.getContext()).setCache(ShareKeyConfig.PING_BAO_CUT_TIME,cutTime);
    }

    /**
     * 开启时间毫秒
     * @return
     */
    public int getOpenTime() {
        return openTime;
    }

    /**
     * 设置开启时间毫秒
     * @param openTime
     */
    public void setOpenTime(int openTime) {
        this.openTime = openTime;
         SharePreferceUtil.getInstance(App.getContext()).setCache(ShareKeyConfig.PING_BAO_OPEN_TIME,openTime);
    }

    /**
     * 更新点击时间
     */
    public void updateClickTime(){
        lastClick = System.currentTimeMillis();
    }

    public boolean getPingBaoOpen() {
        return openPingbao;
    }

    public void setPingBaoOpen(boolean open) {
        openPingbao=open;
        SharePreferceUtil.getInstance(App.getContext()).setCache(ShareKeyConfig.PING_BAO_OPEN, open);
    }


    public void onStop() {
        stop = true;
        install = null;
    }


}
